// import { hdkey } from "ethereumjs-wallet";
// import { generateMnemonic, mnemonicToSeed } from "bip39";
let ethWallet = require("ethereumjs-wallet");
let { generateMnemonic, mnemonicToSeed } = require("bip39");

let Web3 = require("web3");
let words =
  "melody cruel powder satoshi recycle trigger vast minute film alcohol provide opera";
let address = null;
let privateKey = null;

async function init() {
  /*
     使用bip39生成助记词  --》使用助记词生成种子--》生成hdkey-->生成钱包（私钥，公钥），通过钱包获取地址，keystore
  */
  //   let web3 = new Web3(
  //     "https://mainnet.infura.io/v3/7c504602d7a2442cb8ed692deb340e41"
  //   );
  //   words = generateMnemonic(); // 生成助记词
  let seed = await mnemonicToSeed(words); // 生成种子
  console.log("助记词------", words);

  let hdwallet = ethWallet.hdkey.fromMasterSeed(seed); // 生成HD
  let keypair = hdwallet.derivePath("m/44'/60'/0'/0/0"); //.getWallet() // 生成密钥对
  //console.log(keypair, "密钥对");
  let wallet = keypair.getWallet(); // 生成钱包
  console.log(Object.keys(wallet), "钱包");

  address = wallet.getAddressString(); // 生成地址
  console.log("地址-------", address);

  let checkAddress = wallet.getChecksumAddressString(); // 生成校验地址

  privateKey = wallet.getPrivateKey().toString("hex"); // 生成私钥
  console.log("私钥------", privateKey);

  let publicKey = wallet.getPublicKey().toString("hex"); // 生成公钥
  console.log("公钥------", publicKey);

  let keystore = await wallet.toV3("123456"); // 生成keystore
  console.log("keystore---", keystore.address);
}

init();
